<?php
/**
 * core/router.php
 * Application router
 */
// autoloader
function __autoload($className)
{
	// Parse out filename where class should be located
	// This supports names like 'Example_Model' as well as 'Example_Two_Model'
	list($name, $something) = preg_split('/_/', strrev($className), 2);
	$something = strrev($something);
	$name = strrev($name);
	list($functionality, $folder) = preg_split('/_/', strrev($something), 2);
	$folder = strrev($folder);
	$functionality = strrev($functionality);
	switch($folder){
		case 'libraries':
			$path = DOCUMENT_ROOT . '/' .basename('libraries') . '/';
			break;
		case 'driver':
			$path = DOCUMENT_ROOT . '/' . basename($functionality) . '/' . basename('drivers') . '/';
			break;
			case 'models':
				$path = DOCUMENT_ROOT . '/' . basename($functionality) . '/' . basename('models') . '/';
				break;
			case 'data':
				$path = DOCUMENT_ROOT . '/' . basename($functionality) . '/' . basename('data') . '/';
				break;
			case 'view':
				$path = DOCUMENT_ROOT . '/' . basename($functionality) . '/' . basename('view') . '/';
				break;
			case 'driver':
				$path = DOCUMENT_ROOT . '/' . basename($functionality) . '/' . basename('other') . '/';
				break;
			default:
				die("I'm sorry, i don't create absolute path '$functionality/$folder/");
	}
	// create file path
	$file_path = $path . $name . '.php';
	if(file_exists($file_path)){
		include_once($file_path);
	}
	else {
		die("File '$name'.php containts class '$className' does not exists with location: '$path'/'$name'.php!");
	}
}

// fetch the request string
$request_string = $_SERVER['QUERY_STRING'];
// parse the request string witch parsed string
$parsed_string = explode('&', $request_string);
// the name of controller and functionality is the first element
$controller_name = array_shift($parsed_string);
// the rest argument of the array get statemenst and parse them out
$getVarsArray = array();
foreach($parsed_string as $argument){
	// we must split all GET vars along '=' symbol
	// to separate variable and values
	list($variable, $value) = explode('=', $argument);
	$getVarsArray[$variable] = urldecode($value);
}
// create the document path
$controller_folder = basename($controller_name);
$controller_path = DOCUMENT_ROOT . '/' . basename('functionalities') . '/' . $controller_folder . '/' . basename('controllers') . '/' . $controller_folder . '.php';
// get functionality controller
if(file_exists($controller_path)){
	include_once($controller_path);
	// load the controller class
	$controllerClass = ucfirst($controller_name) . '_Controller_' . ucfirst($controller_name);
	if(class_exists($controllerClass)){
		$functionalityController = new $controllerClass;
	}
	else {
		die("Class '$controllerClass' does not exists!");
	}
}
else {
	die("Request page '$controller_name' does not exists!");
}

// initialize post variables
$postVarsArray = $_POST;
$functionalityController->main_function($getVarsArray, $postVarsArray);